package apy.codecase.business.view.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.util.List;

import apy.codecase.App;
import apy.codecase.R;
import apy.codecase.base.BaseActivity;
import apy.codecase.greendao.DiaryDaoUtils;
import apy.codecase.model.DiaryDaoModel;
import apy.codecase.utils.ASPUtils;
import apy.codecase.utils.AToast;
import apy.codecase.utils.Constants;
import apy.codecase.utils.UIUtils;
import apy.codecase.view.LinedEditText;
import butterknife.BindView;
import butterknife.OnClick;

public class AddDiaryActivity extends BaseActivity {

    @BindView(R.id.add_diary_tv_date)
    TextView addDiaryTvDate;
    @BindView(R.id.add_diary_et_title)
    EditText addDiaryEtTitle;
    @BindView(R.id.add_diary_et_content)
    LinedEditText addDiaryEtContent;
    private DiaryDaoUtils diaryDaoUtils;
    private boolean isDel;
    private String content;
    private String title;
    private long itemId;

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, AddDiaryActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected String title() {
        return null;
    }

    @Override
    protected boolean controlTitle() {
        return false;
    }

    @Override
    protected int getViewId() {
        return R.layout.activity_add_diary;
    }

    @Override
    protected void initData() {
        diaryDaoUtils = new DiaryDaoUtils(this);

        Intent intent = getIntent();
        itemId = intent.getLongExtra("itemId",0);
        if(0 != itemId){
            List<DiaryDaoModel> diaryDaoModels = diaryDaoUtils.queryDiaryDaoModelByQueryBuilder(itemId);
            DiaryDaoModel diaryDaoModel = diaryDaoModels.get(0);
            addDiaryEtTitle.setText(diaryDaoModel.getTitle());
            addDiaryEtContent.setText(diaryDaoModel.getContent());
        }
    }

    @OnClick({R.id.diary_add_save, R.id.diary_add_del})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.diary_add_save:
                if(itemId!=0){
                    content = addDiaryEtContent.getText().toString().trim();
                    title = addDiaryEtTitle.getText().toString().trim();
                    if(TextUtils.isEmpty(content)){
                        AToast.showTextToast("您还没有写日记~");
                        return;
                    }
                    if(TextUtils.isEmpty(title)){
                        title = "随手记~";
                    }
                    DiaryDaoModel diaryDaoModel = new DiaryDaoModel();
                    diaryDaoModel.set_id(itemId);
                    diaryDaoModel.setContent(content);
                    diaryDaoModel.setTitle(title);

                    diaryDaoModel.setDate(UIUtils.loadCurrentDate());
                    boolean state = diaryDaoUtils.updateDiaryDaoModel(diaryDaoModel);
                    if(state){
                        AToast.showTextToast("日记修改成功");
                        Intent intent = new Intent();
                        intent.putExtra("data",diaryDaoModel);
                        setResult(300,intent);
                        finish();
                    }
                }else{
                    content = addDiaryEtContent.getText().toString().trim();
                    title = addDiaryEtTitle.getText().toString().trim();
                    if(TextUtils.isEmpty(content)){
                        AToast.showTextToast("您还没有写日记~");
                        return;
                    }
                    if(TextUtils.isEmpty(title)){
                        title = "随手记~";
                    }
                    DiaryDaoModel diaryDaoModel = new DiaryDaoModel();
                    diaryDaoModel.setContent(content);
                    diaryDaoModel.setTitle(title);
                    diaryDaoModel.setId(App.TICKET);
                    diaryDaoModel.setDate(UIUtils.loadCurrentDate());
                    boolean state = diaryDaoUtils.insertDiaryDaoModel(diaryDaoModel);
                    if(state){
                        AToast.showTextToast("日记保存成功");
                        Intent intent = new Intent();
                        intent.putExtra("data",diaryDaoModel);
                        setResult(100,intent);
                        finish();
                    }
                }

                break;
            case R.id.diary_add_del:
                isDel = true;
                finish();
                break;
            default:break;
        }
    }

    @Override
    public void onBackPressed() {
        if(!isDel){
            String title = addDiaryEtTitle.getText().toString().trim();
            String content = addDiaryEtContent.getText().toString().trim();
            if(!TextUtils.isEmpty(title) && !TextUtils.isEmpty(content)){
                Intent intent = new Intent();
                intent.putExtra("1","1");
                setResult(200,intent);
                ASPUtils.saveString(Constants.DIARY_TITLE,title);
                ASPUtils.saveString(Constants.DIARY_CONTENT, content);
                ASPUtils.saveString(Constants.DIARY_DATE,UIUtils.loadCurrentDate());
            }  finish();
        }else {
            super.onBackPressed();
        }
    }
}











